/* 
 * File:   DisplayMonitor.cpp
 * Author: Martijn
 * 
 * Created on 22 juni 2010, 16:43
 */

#include "DisplayMonitor.h"
#include "pRTOS.h"
#include "LCDDisplay.h"  
#include "Clock.h"
#include "string.h"

DisplayMonitor::DisplayMonitor(): cTask("displayMonitor",0 ,100) {	
	std::cout << "Constructed DisplayMonitor\n";    
}

void DisplayMonitor::enable() {
    //resume();
    theLCD.enable();
    theLCD.clear();
}

void DisplayMonitor::disable(void) {
    theLCD.disable();
    suspend();
}

void DisplayMonitor::init(void) {
    enable();
    state = ENABLED;
    theLCD.clear();
    block();
}

void DisplayMonitor::update(void) {
  	switch (state) {
        case ENABLED:
            state = WAITING;
            dmtChan.read();
            break;

        case WAITING:
            di = dmtChan.getItem();
            index = 0;            
            state = FLUSHING;

        case FLUSHING:
            if (index < di.len) {
                theLCD.gotoRowCol(di.row,index);
                theLCD.displayChar(di.buf[index++]);
            } else {
                state = WAITING;
                dmtChan.read();
            }
            break;
    }
    block();
}

void DisplayMonitor::display(DisplayItem di) {
	dmtChan.write(di);
}

void DisplayMonitor::clear(){
  theLCD.clear();
}